<?php
namespace App\Http\Controllers\Member;

use App\Models\BbsAnswer;
use App\Models\BbsAnswerComment;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
/**
 *
 * @author Joker
 *        
 */
class AnswerController extends MemberController
{
    
    public function index()
    {
        $sort = Input::get('sort',0);
        $query = BbsAnswer::with('question')->where('user_id','=',$this->uid);
        switch ($sort){
            case 1:  //回答量
                $query->orderBy('comment_count','desc');
                break;
            default:
                $query->orderBy('id','desc');
        }
        $answer = $query->orderBy('updated_at','desc')->orderBy('created_at','desc')->paginate(10);
        return view('member.bbs.answer')->with('answer',$answer)->with('sort',$sort);
    }
 
    /**
     * 删除
     */
    public function destroy($id)
    {
        $answer = BbsAnswer::where(array('id'=>$id,'user_id'=>$this->uid))->first();
        if(empty($answer))
        {
            return  $this->formatJson('falied','抱歉，资源不存在!');
        }
        else
        {
            DB::transaction(function() use ($answer){
                BbsAnswerComment::where(array('answer_id'=>$answer->id,'user_id'=>$this->uid))->delete();
                $answer->delete();
            });
            return $this->formatJson('success','资源删除成功！');
        }
    }
}